﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace NewsletterCreator
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:NewsletterGenerator runat=server></{0}:NewsletterGenerator>")]
    public class NewsletterGenerator : CompositeControl
    {
        private Panel pnlControlLayout = new Panel();
        private Panel pnlOptions = new Panel();
        private Panel pnlhtmlpreview = new Panel();
        private Label lblheader = new Label();
        private Label lblpreview = new Label();
        private Button btnPreview = new Button();
        private TextBox txtPreamble = new TextBox();
                
        private string organizationname;
        private int numColumns =2;

        private HtmlableControl[] ctrlColumns = {new NumberedNewsletterItem(),new NumberedNewsletterItem()};

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Text
        {
            get
            {
                String s = (String)ViewState["Text"];
                return ((s == null) ? String.Empty : s);
            }

            set
            {
                ViewState["Text"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("[Organization name]")]
        [Description("The name of the organization sending out the newsletter")]
        [Localizable(true)]
        public string OrganizationName
        {
            get { return organizationname; }
            set { organizationname = value; }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("2")]
        [Description("The number of columns in the newsletter")]
        [Localizable(true)]
        public int NumberOfColumns
        {
            get { return numColumns; }
            set
            {
                numColumns = value;
                if (ctrlColumns == null)
                {
                    ctrlColumns = new HtmlableControl[numColumns];
                    for (int i = 0; i < numColumns; i++)
                        ctrlColumns[i] = new NumberedNewsletterItem();
                }
            }
        }
                
        public void AddPanelControl(HtmlableControl child, int column)
        {
            if (ctrlColumns == null)
                ctrlColumns = new HtmlableControl[numColumns];

            ctrlColumns[column] = child;
            //if(column+1 == numColumns)
            //    foreach (HtmlableControl ctrl in ctrlColumns)
            //        pnlOptions.Controls.Add(ctrlColumns[0]);
        }
              

        //protected override void RenderContents(HtmlTextWriter output)
        //{
        //    output.Write("Text");
        //}
        protected override void Render(HtmlTextWriter output)
        {
            base.Render(output);
        }
        protected override void CreateChildControls()
        {
            btnPreview.Text = "Preview";
            Label lblGeneratorHeader = new Label();
            lblGeneratorHeader.Text = "<b>Newsletter Generator</b>";
            lblheader.Text = "Introduction/Preamble to the newsletter: ";
            txtPreamble.Width = 500;
            txtPreamble.Height = 100;
            txtPreamble.TextMode = TextBoxMode.MultiLine;
            
            btnPreview.Click += new EventHandler(this.onPreviewClick);

            pnlOptions.BorderWidth = 1;

            pnlOptions.Controls.Add(lblheader);
            pnlOptions.Controls.Add(new LiteralControl("<BR />"));
            pnlOptions.Controls.Add(txtPreamble);
            pnlOptions.Controls.Add(new LiteralControl("<BR />"));

            for(int i =0; i<numColumns; i++)
                pnlOptions.Controls.Add(ctrlColumns[i]);

            pnlOptions.Controls.Add(new LiteralControl("<BR />"));
            pnlOptions.Controls.Add(btnPreview);

            lblpreview.Text = generateHTML();

            pnlhtmlpreview.BorderWidth = 1;
            pnlhtmlpreview.Controls.Add(lblpreview);

            pnlControlLayout.BorderWidth = 1;

            pnlControlLayout.Controls.Add(lblGeneratorHeader);
            pnlControlLayout.Controls.Add(pnlOptions);
            pnlControlLayout.Controls.Add(pnlhtmlpreview);

            Controls.Clear();
            Controls.Add(pnlControlLayout);
        }
        protected void onPreviewClick(Object sender, EventArgs e)
        {
            lblpreview.Text = generateHTML();
        }

        public String generateHTML()
        {
            String str = "";
            str += "<h3><center>" + organizationname + "</center></h3>";

            String[] lines = txtPreamble.Text.Split('\n');
            foreach (String line in lines)
                str += "<p>"+line+"</p>";

            str += "<table><tr>";
            for (int i = 0; i < numColumns; i++)
                str += "<td valign=\"top\">" + ctrlColumns[i].toHTML() + "</td>";

            str += "</tr></table>";

            return str;
        }

    }
}
